package com.example;

/**
 * @desc:
 * @author: Shoko
 * @date: 2025/6/6
 */
import java.util.Stack;

public class ContactsApp {
    private AddressBook addressBook = new AddressBook();
    private Stack<ResourceCommand> history = new Stack<>();

    public void add(String name, String phone) {
        ResourceCommand command = new AddCommand(addressBook, name, phone);
        command.execute();
        history.push(command);
    }

    public void delete(String name) {
        ResourceCommand command = new DeleteCommand(addressBook, name);
        command.execute();
        history.push(command);
    }

    public void view(String name) {
        ResourceCommand command = new ViewCommand(addressBook, name);
        command.execute();
        // 查看操作通常不加入历史记录，因为不可撤销
    }

    public void undo() {
        if (!history.isEmpty()) {
            ResourceCommand command = history.pop();
            command.undo();
        } else {
            System.out.println("没有可撤销的操作");
        }
    }
}
